﻿using System;
using System.Collections.Generic;

namespace SBPweb.Modules.Smartparts
{
    /// <summary>
    /// This generic class represents a base class for menu.
    /// </summary>
    public abstract class MenuBase : List<IMenuItem>//<T> : List<T>, IMenuList where T : IMenuItem
    {

        #region Constructor logic

		/// <summary>
		/// Inizializes a new MenuBase instance
		/// </summary>
        protected MenuBase()
        {
        }

        #endregion

        #region IMenuList Members

        /// <summary>
        /// Gets an array of menu items.
        /// </summary>
        public IMenuItem[] MenuItems
        {
            get
            {
                return base.ToArray(); //Array.ConvertAll<T, IMenuItem>(base.ToArray(), new Converter<T, IMenuItem>(ConvertTToIMenuItem));
            }
        }

        /// <summary>
        /// Occurs when a new item is added to the list.
        /// </summary>
        public event EventHandler<MenuEventArgs> ItemAdded;

        /// <summary>
        /// Occurs when an item is removed from the list.
        /// </summary>
        public event EventHandler<MenuEventArgs> ItemRemoved;

        #endregion

        #region Helper methods

        /// <summary>
        /// Returns a converted instance of the parameter.
        /// </summary>
        /// <param name="t">The generic type.</param>
        /// <returns></returns>
        //public static IMenuItem ConvertTToIMenuItem(IMenuItem/*T*/ t)
        //{
        //    return (IMenuItem)t;
        //}

        #endregion

        #region IList<IMenuItem> Members

        /// <summary>
        /// Returns the index of the given item.
        /// </summary>
        /// <param name="item">The menuitem to look for.</param>
        /// <returns></returns>
        //public int IndexOf(IMenuItem item)
        //{
        //    return base.IndexOf(/*(T)*/item);
        //}

        /// <summary>
        /// Inserts a new menu item to the specified position.
        /// </summary>
        /// <param name="index">The position of the new item.</param>
        /// <param name="item">The nem menuitem instance.</param>
        public new void Insert(int index, IMenuItem item)
        {
            base.Insert(index, /*(T)*/item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new MenuEventArgs(item));
            }
        }

        /// <summary>
        /// Indexer for the list. Gets or sets the specified item.
        /// </summary>
        /// <param name="index">The spcified position in the list.</param>
        /// <returns></returns>
        public new IMenuItem this[int index]
        {
            get
            {
                return (IMenuItem)base[index];
            }
            set
            {
                base[index] = /*(T)*/value;
            }
        }

        #endregion

        #region ICollection<IMenuItem> Members

        /// <summary>
        /// Adds the specified item to the list.
        /// </summary>
        /// <param name="item">The menu item instance to be added.</param>
        public new void Add(IMenuItem item)
        {
            base.Add(/*(T)*/item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new MenuEventArgs(item));
            }
        }

        /// <summary>
        /// Indicates whether the list contains the specified smartpart.
        /// </summary>
        /// <param name="item">The smartpart to look for.</param>
        /// <returns></returns>
        //public bool Contains(IMenuItem item)
        //{
        //    return base.Contains(/*(T)*/item);
        //}

        /// <summary>
        /// This method is not implemented yet!
        /// </summary>
        /// <param name="array">The source array.</param>
        /// <param name="arrayIndex">The starting index.</param>
        //public void CopyTo(IMenuItem[] array, int arrayIndex)
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// Gets a value indicating whether the list is read only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Removes the specified item from the list.
        /// </summary>
        /// <param name="item">The menu item to be removed.</param>
        /// <returns></returns>
        public new bool Remove(IMenuItem item)
        {
            bool ret = base.Remove(/*(T)*/item);
            if (ItemRemoved != null)
            {
                ItemRemoved.Invoke(this, new MenuEventArgs(item));
            }
            return ret;
        }

        #endregion

        #region IEnumerable<IMenuItem> Members
        
        /// <summary>
        /// Gets the enumeration of the list.
        /// </summary>
        /// <returns></returns>
        //public new IEnumerator<IMenuItem> GetEnumerator()
        //{
        //    throw new NotImplementedException();
        //}

        #endregion
    }

}